<?php
class HotelImagexController extends CBaseController
{		
	public function __construct($config=null) 
    { 
      parent::__construct($config);	  
    } 
	
	public function init($tpl)
	{
		parent::init($tpl);
		$this->reqLogin();
		$this->templator->addViewsDir(dirname(__FILE__).'/../views');
		$this->templator->assign('menu','imagex');
	}
	
	public function assign()
	{
		$id = $_POST['id'];
		$imagex_ids = $_POST['imagex_ids'];
		$hotel = R::load('hotel',$id);		
		foreach($imagex_ids as $imagex_id)
		{
			$imagex = R::load('image',$imagex_id);
			R::associate( $hotel, $imagex );
		}
		$this->imagexs($id);
	}
	
	public function unassign()
	{
		$id = $_GET['id'];
		$hotel = R::load('hotel',$id);
		$imagex_id = $_GET['imagex_id'];
		$imagex = R::load('image',$imagex_id);
		R::unassociate( $hotel, $imagex );
		$this->imagexs();
	}
	
	public function imagexs($id=null)
	{
		if(!isset($id))
		$id = $_GET['id'];
		$hotel = R::load('hotel',$id);
		$assignedImagexs= R::related( $hotel, 'image' );
		$this->templator->assign('hotel',$hotel);		
		$this->templator->assign('imagexs',array_merge($assignedImagexs, array()));
		$this->templator->display('imagex/hotel_imagex.html');
	}
	
	public function select()
	{
		$id = $_POST['id'];
		$hotel = R::load('hotel',$id);
		$assignedImagexs= R::related( $hotel, 'image' );		
		$allImagexs = R::findAll('image');
		$imagexs =array_diff($allImagexs, $assignedImagexs);
		$this->templator->assign('hotel',$hotel);
		$this->templator->assign('imagexs',array_merge($imagexs, array()));
		$this->templator->display('imagex/imagex_list.html');
	}
	
}	